<!-- 政府管理 -->
<template>
  <div class="mod-config">
      <el-form inline :model="dataForm" v-if="searchFormShow">
        <el-row class="bg-row">
          <el-col :span="6">
            <el-form-item label="政府名称">
              <el-input v-model="dataForm.governmentName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="省名称">
              <el-select filterable default-first-option clearable v-model="dataForm.province" placeholder="请选择" @change="getCitys">
                <el-option v-for="item in provinces" :key="item.id" :label="item.regionName"
                  :value="item.id">
                  <span style="float: left">{{ item.regionName }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.id }}</span>
                </el-option>
              </el-select>
              <!-- <el-input v-model="dataForm.province"></el-input> -->
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="市名称">
              <el-select filterable default-first-option clearable v-model="dataForm.city" placeholder="请选择" @change="getAreas">
                <el-option v-for="item in citys" :key="item.id" :label="item.regionName"
                  :value="item.id">
                  <span style="float: left">{{ item.regionName }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.id }}</span>
                </el-option>
              </el-select>
              <!-- <el-input v-model="dataForm.city"></el-input> -->
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="区/县名称">
              <el-select filterable default-first-option clearable v-model="dataForm.area" placeholder="请选择">
                <el-option v-for="item in areas" :key="item.id" :label="item.regionName"
                  :value="item.id">
                  <span style="float: left">{{ item.regionName }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.id }}</span>
                </el-option>
              </el-select>
              <!-- <el-input v-model="dataForm.area"></el-input> -->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="bg-row">
            <el-form-item>
              <el-button size="mini" icon="el-icon-search" type="primary" @click="searchHandle">查询</el-button>
              <el-button size="mini" icon="el-icon-delete" type="info" @click="clearSearchForm">清空</el-button>
            </el-form-item>
        </el-row>
      </el-form>
      <div style="margin-top: 5px;margin-bottom: 5px;">
        <el-row style="margin-top: 5px;">
          <el-button icon="el-icon-plus" type="primary" @click="addOrUpdateHandle('','add')">新增</el-button>
          <el-tooltip class="item" effect="dark" content="搜索" placement="top"style="float: right;">
            <el-button icon="el-icon-search" circle @click="searchFormShow = !searchFormShow"></el-button>
          </el-tooltip>
        </el-row>
      </div>
      <el-table :data="dataList" height="calc(100vh - 215px)" border stripe v-loading="dataListLoading">
        <el-table-column type="index" label="序号" width="40"></el-table-column>
        <el-table-column prop="governmentName" label="政府名称" width="160"></el-table-column>
        <el-table-column prop="province" label="省名称" width="120"></el-table-column>
        <el-table-column prop="city" label="市名称" width="120"></el-table-column>
        <el-table-column prop="area" label="区/县名称" width="120"></el-table-column>
        <el-table-column prop="address" label="详细地址" width="180"></el-table-column>
        <el-table-column prop="fullAddress" label="全地址" min-width="180"></el-table-column>

        <el-table-column label="操作" width="230" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-view" @click="addOrUpdateHandle(scope.row.id,'check')">查看</el-button>
            <el-button type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id,'edit')">编辑</el-button>
            <el-button type="text" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="avue-crud__pagination" style="margin-bottom: 25px;">
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100, 200]"
          :page-size="pageSize"
          :total="totalPage"
          background
          layout="total, sizes, prev, pager, next">
        </el-pagination>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
  </div>
</template>

<script>
  import {filterParams} from '@/util/tools'
  import TableForm from './TableForm'
  import {mapGetters} from 'vuex'
  import {fetchList, delObj} from '@/api/pvm/government'
  import { getListByParentId } from '@/api/admin/region'

  export default {
    data () {
      return {
        searchFormShow: true,
        dataForm: {
          governmentName: '',
          province: '',
          city: '',
          area: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 200,
        totalPage: 0,
        provinces: [],
        citys: [],
        areas: [],
        dataListLoading: false,
        addOrUpdateVisible: false
      }
    },
    components: {
      TableForm,
    },
    created () {
      this.searchHandle()
      this.getProvinces()
    },
    computed: {
      ...mapGetters(['permissions','userInfo'])
    },
    methods: {
      //  查询数据
      searchHandle() {
        this.pageIndex = 1
        this.getDataList()
      },
      //  清空搜索表单
      clearSearchForm() {
        for(let key in this.dataForm){
          if(key == 'allocationType'){
            this.dataForm[key] = '2'
          }else{
            this.dataForm[key] = ''
          }
        }
      },
      // 获取数据列表
      getDataList () {
        let params1 = {current: this.pageIndex, size: this.pageSize}
        let params2 = filterParams(this.dataForm)
        this.dataListLoading = true
        fetchList(Object.assign(params1,params2)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增 / 编辑 / 查看
      addOrUpdateHandle (id,type) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id,type)
        })
      },
      // 删除
      deleteHandle (id) {
        this.$confirm('是否确认删除ID为' + id, '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(function () {
          return delObj(id)
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success('删除成功')
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      //  日期格式
      dateFormat(dateStr) {
        if(dateStr){
          return dateStr.substring(0,16)
        }
      },
      // 生成省级列表
      getProvinces(){
        var parentId = '86';
        getListByParentId(parentId).then(response => {
          if(response.data.code == '0'){
            this.provinces = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        }).catch(() => {
          alert("省级地址出错了，请联系管理员")
        })
      },
      getCitys(id){
        this.dataForm.city = ''
        this.dataForm.area = ''
        this.citys = []
        this.areas = []
        this.$set(this.dataForm,'city','')
        this.$set(this.dataForm,'area','')
        if(id){
          getListByParentId(id).then(response => {
            if(response.data.code == '0'){
              this.citys = response.data.data
            }else{
              this.$message.error(response.data.msg)
            }
          }).catch(() => {
            alert("省级地址出错了，请联系管理员")
          })
        }
      },
      getAreas(id){
        this.areas = []
        this.$set(this.dataForm,'area','')
        if(id){
          getListByParentId(id).then(response => {
            if(response.data.code == '0'){
              this.areas = response.data.data
            }else{
              this.$message.error(response.data.msg)
            }
          }).catch(() => {
            alert("省级地址出错了，请联系管理员a ")
          })
        }
      }
    }
  }
</script>
